#include "plugins.h"

#define JSON_COMPONENTS_PATH "component"
#define INDEX_TAG            ":"
#define JSON_ITEM            256

CPlugins::CPlugins(void)
{
    m_pdata.dispose();
}
CPlugins::~CPlugins(void)
{
}
HRESULT CPlugins::Clear()
{
    HRESULT hr = S_OK;

    m_pJson.dispose();
    m_pdata.dispose();
    m_SortVecPlugins.clear();
    m_mapComponents.clear();

    return hr;
}
HRESULT CPlugins::LoadMemory(LPCSTR buf, ULONG len)
{
    rc_error(buf != NULL, E_FAIL);

    m_pJson = sjson_parse(buf, 0, 0);
    rc_error(m_pJson.m_p != NULL, E_FAIL);
    return LoadPluginModule(m_pJson);
}
DWORD CPlugins::GetClsidCount()
{
    return m_SortVecPlugins.size();
}

DWORD CPlugins::GetCompentsCount()
{
    return m_mapComponents.size();
}

const ComponentPlugin& CPlugins::LoadCompents()
{
    return m_mapComponents;
}

DWORD CPlugins::LoadClsidAll(PluginVector& vecPlugins)
{
    DWORD dwCount = GetClsidCount();

    vecPlugins.clear();

    for (UINT uloop = 0; uloop < dwCount; uloop++) {
        vecPlugins.push_back_sort(m_SortVecPlugins[uloop]);
    }

    return vecPlugins.size();
}
DWORD CPlugins::LoadClsid(PluginVector& vecPlugins, const char* pComponents)
{
    DWORD dwCount = GetClsidCount();

    vecPlugins.clear();

    for (UINT uloop = 0; uloop < dwCount; uloop++) {
        if (string_stricmp(pComponents, m_SortVecPlugins[uloop].strComponent.c_str()) == 0) {
            vecPlugins.push_back(m_SortVecPlugins[uloop]);
        }
    }
    return vecPlugins.size();
}

HRESULT CPlugins::LoadPluginModule(sjsoncptr JsRoot)
{
    HRESULT hr = S_OK;

    rc_error(JsRoot != NULL, E_FAIL);

    if (sjson_is_obj(JsRoot) == sjson_true) {
        sjsonptr JsModule = sjson_get_obj(JsRoot, JSON_COMPONENTS_PATH);

        if (sjson_is_array(JsModule) == sjson_true) {
            int iloop = 0;
            for (iloop = 0; iloop < sjson_get_array_size(JsModule); iloop++) {
                sjsonptr JsItem = NULL;
                JsItem = sjson_get_array_item(JsModule, iloop);
                LoadPluginItem(JsItem);
            }
        }
    }

    return hr;
}

HRESULT CPlugins::LoadPluginItem(sjsoncptr JsRoot)
{
    HRESULT hr = S_OK;

    if (sjson_is_string(JsRoot) == sjson_true) {
        CharArrayPtr item;
        item.dispose();
        item = ALLOC_NEW char[JSON_ITEM + 1]();
        rc_error(item.m_p != NULL, E_FAIL);

        rc_error(JsRoot->valuestring != NULL, E_FAIL);
        rc_error(JsRoot->valuestring[0] != 0x00, E_FAIL);

        s_strcpy(item.m_p, JSON_ITEM, JsRoot->valuestring);

        char* next = NULL;
        char* clsid = NULL;
        char* component = NULL;
        char* index = NULL;
        char* off = NULL;

        clsid = s_strtok(item, INDEX_TAG, &next);
        rc_error(clsid != NULL, E_FAIL);

        component = s_strtok(NULL, INDEX_TAG, &next);
        rc_error(component != NULL, E_FAIL);

        index = s_strtok(NULL, INDEX_TAG, &next);
        rc_error(index != NULL, E_FAIL);

        off = s_strtok(NULL, INDEX_TAG, &next);
        rc_error(off != NULL, E_FAIL);

        m_mapComponents[component] = off;
        m_SortVecPlugins.push_back(Com_Plugin(StringToGUID(clsid), atol(index), component));
    }

    return hr;
}

HRESULT CPlugins::GetItemString(sjsoncptr JsRoot, const char* id, char** pItem)
{
    HRESULT hr = E_FAIL;

    if (sjson_is_obj(JsRoot)) {
        sjsonptr JsItem = sjson_get_obj(JsRoot, id);

        if (sjson_is_array(JsItem)) {
            *pItem = JsItem->valuestring;
            hr = S_OK;
        }
    }
    return hr;
}
